﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.Threading;
using EntityFramework.Extensions;
using System.Data.Objects.SqlClient;
namespace Webcar.Controllers
{
    public partial class CarController
    {
        public ActionResult CostList()
        {
            return View();
        }
        
        public ActionResult CostListPartialView(string id)
        {
            return PartialView();
        }
        public ActionResult CostEdit(string carno, string month, string deptcode)
        {
            CarCost cost = null;
            if (carno.IsNotNull())
            {
                cost = WebcarDataProvider.DB.CarCost.Find(carno, month);
            }
            if (cost == null)
            {
                cost = new CarCost();
                cost.dept_Code = deptcode;
                cost.ny = DateTime.Now.ToString("yyyy-MM");
            }
            ViewBag.RecordNo = null;
            if (cost.carno.IsNotNull())
            {
                var car = WebcarDataProvider.DB.BS_CAR_RECORD.Find(cost.carno);
                if (car != null)
                {
                    ViewBag.RecordNo = car.RECORD_NO;
                }
            }
            return View(model: cost);
        }
        public ActionResult CostSave(CarCost cost)
        {
            CurrentUser.EnsurePermission(PermissionManager.编辑单车成本);
            if (cost.ny.IsNull() || cost.carno.IsNull())
            {
                return Content("请选择车辆和输入月份。");
            }
            if ((cost.ny + "-01").IsDate() == false)
            {
                return Content("月份格式错误。格式示例：2010-10");
            }
            using (var DB = new WebcarContext())
            {
                var item = DB.CarCost.Find(cost.carno, cost.ny);
                if (item == null)
                {
                    DB.CarCost.Add(cost);
                }
                else
                {
                    cost.CopyPropertyTo(item);
                }
                DB.SaveChanges();
            }
            return RedirectToAction("CostList", new { id = cost.dept_Code });
            //return SuccessAlertThenReload();
            
        }

        public ActionResult CostDelete(string car_no,string month,string deptcode)
        {
            CurrentUser.CheckPermission(PermissionManager.编辑单车成本);
            using (var DB = new WebcarContext())
            {
                var cost = DB.CarCost.Delete(a => a.carno == car_no && a.ny == month);

                DB.SaveChanges();
            }
            //return new RedirectResult()
            //return RedirectToRoute("List", "Insure", new { id = id });
            return RedirectToAction("CostList", "Car", new { id = deptcode });
        }

        public ActionResult CostSummary()
        {
            string start_date = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
            string end_date = DateTime.Now.ToString("yyyy-MM");
            ViewBag.StartDate = start_date;
            ViewBag.EndDate = end_date;

            if (this.Request.Form["start_date"] != null && this.Request.Form["end_date"] != null)
            {
                start_date = this.Request.Form["start_date"];
                end_date = this.Request.Form["end_date"];
            }
            string sql = @"SELECT a.[dept_code],b.deptname 
              ,sum([zs_fee]) zs_fee
              ,sum([cl_fee]) [cl_fee] 
              ,sum([lq_fee]) [lq_fee]
              ,sum([xc_fee]) [xc_fee]
              ,sum([tc_fee]) [tc_fee]
              ,sum([ns_fee]) [ns_fee]
              ,sum([lq_year]) [lq_year]
              ,sum([lq_month]) [lq_month]
              ,sum([road_fee]) [road_fee]
              ,sum([oil_fee]) [oil_fee]
              ,sum([other_fee]) [other_fee]
              ,sum([oil_nums]) [oil_nums]
              ,sum([oil_price]) [oil_price]
              ,sum([sum_fee])  [sum_fee]
               FROM [webcar].[dbo].[CarCost] a,sys_dept b where a.dept_code=b.deptcode
               and a.month between '{0}' and '{1}' and b.use_flag='T'
               group by b.deptname,a.[dept_code]".FormatWith(start_date,end_date);
            var table = WebcarDataProvider.DB.ExecuteDataTable(sql);
            ViewBag.StartDate = start_date;
            ViewBag.EndDate = end_date;

            return View(model: table);
        }
    }
}